package aom.ju.ss.view.dicker

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import aom.ju.ss.R
import aom.ju.ss.databinding.BarterGoodsItemBinding
import aom.ju.ss.mode.YHGoods
import aom.ju.ss.mode.YHGoodsData
import aom.ju.ss.utils.dp
import aom.ju.ss.utils.http.LP_API.get_list
import aom.ju.ss.utils.show
import aom.ju.ss.utils.start
import aom.ju.ss.view.base.BaseListFragment
import aom.ju.ss.view.cut.CutGoodsDetailsActivity
import com.google.gson.Gson

/**
 * 首页易货 商品 item fragment
 */
class BarterGoods : BaseListFragment() {

    companion object {
        fun getMys(id: String, city: String?): BarterGoods {
            val f = BarterGoods()
            val b = Bundle()
            b.putString("id", id)
            b.putString("city", city)
            f.arguments = b
            return f
        }
    }

    var city = ""
    override fun bind(binding: ViewDataBinding, position: Int) {
        super.bind(binding, position)
        val bin = binding as BarterGoodsItemBinding
        val info = getList()[position] as YHGoodsData
        bin.info = info

        bin.rootView.setOnClickListener {
            start(CutGoodsDetailsActivity::class.java, info.id.toString())
        }
    }

    override fun item(): Int {
        return R.layout.barter_goods_item
    }

    override fun LayoutManager(): RecyclerView.LayoutManager? {
        return GridLayoutManager(activity, 2)
    }

    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        val mode = Gson().fromJson(jsonString, YHGoods::class.java)
            setList(mode.data)
    }


    override fun error(e: String?, jsonString: String?, httpTY: String?) {
        super.error(e, jsonString, httpTY)
        e?.show
    }

    override fun onBaseCreateView() {
        super.onBaseCreateView()
        binding?.small?.setPadding(12.dp, 12.dp, 12.dp, 0)
        binding?.img?.setImageResource(R.drawable.no_goods)
        binding?.msg?.text = "找到相关商品"
        binding?.rootView?.setBackgroundResource(R.color.f9f9f9)
//        binding?.small?.setEnableRefresh(false)

    }

    override fun api(): String {
        return get_list
    }

    override fun key(): Array<String> {
        return arrayOf("type", "city", "category_level", "category_id")
    }

    var id = ""
    override fun value(): Array<String> {
        arguments?.getString("city")?.let {
            city = it
        }

        arguments?.getString("id")?.let {
            id = it
        }

        return arrayOf("4", city, "1", id)
    }

    init {
        isShow = false

    }


}